package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO: 增加描述
 *
 * @author li.xh
 * @version 1.0.0
 * @date 2019-12-13 10:34
 * @copyright wonhigh.cn
 */
@RestController
@RequestMapping("/role")
public class RoleController {


    @Reference
    private RoleService roleService;

    @Reference
    private PermissionService permissionService;

    @Reference
    private MenuService menuService;
    //新增角色
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        try{
            roleService.add(role,permissionIds,menuIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);//新增失败
        }
        return new Result(true,MessageConstant.ADD_USER_SUCCESS);//新增成功
    }

    @RequestMapping("/delete")
    public Result deleteById(Integer id){
        try{
            roleService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.pageQuery(queryPageBean);
    }

    //根据ID查询角色
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Role role = roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,role);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);//查询失败
        }
    }
    //根据角色ID查询角色包含的多个权限ID
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try{
            List<Permission> permissionIds_1 = permissionService.findPermissionIdsByRoleId(id);
            List<Integer> permissionIds = new ArrayList<>();
            for (Permission permission : permissionIds_1) {
                permissionIds.add(permission.getId());
            }
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,permissionIds);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);//查询失败
        }
    }


    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        try{
            List<Menu> menuIds_1 = menuService.findMenuIdsByRoleId(id);
            List<Integer> menuIds = new ArrayList<>();
            for (Menu menu : menuIds_1) {
                menuIds.add(menu.getId());
            }
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,menuIds);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);//查询失败
        }
    }


    @RequestMapping("/findAllPermission")
    public Result findAllPermission(){
        try{
            List<Permission> permissions = permissionService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,permissions);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);//查询失败
        }
    }

    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){
        try{
            List<Menu> menus = menuService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,menus);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);//查询失败
        }
    }
    //编辑角色
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try{
            roleService.edit(role,permissionIds,menuIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);//新增失败
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);//新增成功
    }

    //查询所有角色
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Role> list = roleService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);//查询失败
        }
    }
}
